﻿Imports System.Text

Public Class CDRViewerReport

    Inherits [lib].BasePage

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
    Protected WithEvents DataGrid2 As System.Web.UI.WebControls.DataGrid
    Protected WithEvents ContentPaneFooter1 As ccapps.BillingNetControlLibrary.ContentPaneFooter

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.PageTitle = "Bulk CDR Viewer"

        If (Not Me.IsPostBack) Then
            If Me.Request.QueryString("RequestID") Is Nothing Then
                listHistory(0)
            Else
                listDetail(0)
            End If
        End If
 
        Me.DataGrid1.Visible = Request.QueryString("RequestID") Is Nothing
        Me.DataGrid2.Visible = Not Me.DataGrid1.Visible
        Me.ContentPaneFooter1.ConfirmButton.Visible = (Me.DataGrid1.Visible AndAlso Me.DataGrid1.Items.Count > 0) OrElse (Me.DataGrid2.Visible AndAlso Me.DataGrid2.Items.Count > 0)
        Me.ContentPaneFooter1.NavigateBackClientFunction = String.Format("javascript:location.href='{0}'", IIf(Me.DataGrid1.Visible, "CDRViewer.aspx", "CDRViewerReport.aspx"))
    End Sub

    Private Sub CheckAuthorizied()

        Dim role As Integer
        role = CInt(Session("role_id"))
        Session("IsAuthoriziedFRM") = Nothing

        Dim query As String = "select item_name From EU_BILL.ITEMS_WHICH_BE_MASKED Where item_id in (select FORM_CONTROL_ITEM_ID from billintra.form_control_authorization fca where fca.FORM_CONTROL_ID = 11 and role_id= " & role & " )"


        If persistent.myDBConn.myORATable(query).Rows.Count = 0 Then
            Session("IsAuthoriziedFRM") = ""
        Else
            Session("IsAuthoriziedFRM") = persistent.myDBConn.myORATable(query).Rows(0)(0)
        End If


    End Sub


    Private Sub listHistory(ByVal pageIndex As Integer)
        Dim message As String = String.Empty

        Try

            Me.DataGrid1.CurrentPageIndex = pageIndex
            Me.DataGrid1.DataSource = persistent.CDRViewer.GetRequestList(-1, message)
            Me.DataGrid1.DataBind()

            If message <> String.Empty Then
                Alert(message, Me)
            End If

        Catch ex As Exception
            Alert(ex.Message, Me)
        End Try

    End Sub

    Private Sub listDetail(ByVal pageIndex As Integer)
        Dim message As String = String.Empty

        Try

            Me.DataGrid2.CurrentPageIndex = pageIndex
            Me.DataGrid2.DataSource = persistent.CDRViewer.GetRequestDetail(Request.QueryString("RequestID"), message)
            Me.DataGrid2.DataBind()

            If message <> String.Empty Then
                Alert(message, Me)
            End If

        Catch ex As Exception
            Alert(ex.Message, Me)
        End Try

    End Sub

    Private Sub DataGrid1_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DataGrid1.PageIndexChanged
        Me.listHistory(e.NewPageIndex)
    End Sub

    Private Sub DataGrid2_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DataGrid2.PageIndexChanged
        Me.listDetail(e.NewPageIndex)
    End Sub


    Private Sub DataGrid1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.ItemCommand
        Dim message As String = String.Empty

        Select Case e.CommandName
            Case "CancelRequest"
                If persistent.CDRViewer.CancelRequest(e.CommandArgument, message) Then
                    Me.listHistory(Me.DataGrid1.CurrentPageIndex)
                Else
                    Alert(message, Me)
                End If
            Case "Reproccess"
                If persistent.CDRViewer.GenerateReport(e.CommandArgument, message) Then
                    Me.listHistory(Me.DataGrid1.CurrentPageIndex)
                Else
                    Alert(message, Me)
                End If
            Case "GetOutput"
                Dim fileContent As String = persistent.CDRViewer.GetRequestOutput(e.CommandArgument, ";", message)
                Me.CheckAuthorizied()

                If Session("IsAuthoriziedFRM").ToString().IndexOf("FRM Tool") < 0 Then
                    fileContent = Me.MaskTheMsisdns(fileContent)
                End If

                Me.setFiletoDownload("CDR_Bilgileri.csv", fileContent)
            Case "ShowRequestDetails"
                Response.Redirect("CDRViewerReport.aspx?RequestID=" & e.CommandArgument)
        End Select

    End Sub

    Private Sub ContentPaneFooter1_Confirmed(ByVal sender As Object, ByVal e As System.EventArgs) Handles ContentPaneFooter1.Confirmed
        Dim dataGrid As DataGrid = IIf(Request.QueryString("RequestId") Is Nothing, Me.DataGrid1, Me.DataGrid2)
        Dim currentPageIndex = dataGrid.CurrentPageIndex
        dataGrid.AllowPaging = False

        If dataGrid Is Me.DataGrid1 Then
            Me.DataGrid1.Columns(Me.DataGrid1.Columns.Count - 1).Visible = False
            Me.listHistory(0)
        Else
            Me.listDetail(0)
        End If


        Dim stringWrite As New System.IO.StringWriter

        Dim htmlWrite As New HtmlTextWriter(stringWrite)

        dataGrid.RenderControl(htmlWrite)

        Me.setFiletoDownload("Requests.xls", stringWrite.ToString())

        dataGrid.AllowPaging = True

        If dataGrid Is Me.DataGrid1 Then
            Me.DataGrid1.Columns(Me.DataGrid1.Columns.Count - 1).Visible = True
            Me.listHistory(currentPageIndex)
        Else
            Me.listDetail(currentPageIndex)
        End If

    End Sub

    Private Sub setFiletoDownload(ByVal fileName As String, ByVal fileContent As String)

        Response.Clear()
        Response.Buffer = True
        Response.AddHeader("content-disposition", "attachment;filename=" & fileName)

        Response.Cache.SetCacheability(HttpCacheability.NoCache)

        Response.ContentType = "application/vnd.ms-excel"

        Response.Charset = ""

        Me.EnableViewState = False

        Response.Write(fileContent.Replace("�", ""))

        Response.End()

    End Sub

    Private Function MaskTheMsisdns(ByVal fileContent As String) As String

        Dim values As String() = fileContent.Split(";"c)

        Dim x As Integer = 0

        For Each line As String In values

            If values(x).Length >= 10 And IsNumeric(values(x)) Then

                values(x) = values(x).Substring(0, 2) + "*******" + values(x).Substring(values(x).Length - 2, 2)

            End If
            x = x + 1

        Next

        fileContent = fileContent.Join(";"c, values)

        Return fileContent

    End Function


End Class
